﻿using Crm.Infrastructure.Dto.Request.Dictionary;
using Crm.Infrastructure.Dto.Response;
using Crm.Infrastructure.Dto.Response.Dictionary;
using Crm.Service.Interface;
using CRM.Api.Admin.Controllers;
using CRM.Infrastructure.Dto.Request.Dictionary;
using CRM.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace Crm.Api.Admin.Controllers
{


    public class DictionaryController : BaseController
    {
        private readonly I_Dictionary_Service _dictionary_Service;

        public DictionaryController(I_Dictionary_Service dictionary_Service, I_Login_Service login_Service) : base(login_Service)
        {
            _dictionary_Service = dictionary_Service;

        }

        /// <summary>
        /// 获取字典类别列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [Authorize(Roles = "admin")]
        [HttpGet]
        public async Task<IActionResult> Get_Dictionary_Types(int? page = 1, int? limit = 10, string? key = "")
        {
            var result = await _dictionary_Service.Get_Dictionaty_Types(page ?? 1, limit ?? 10, key ?? "");
            return Ok(result);
        }

        /// <summary>
        /// 删除字典类别[ids]
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<IActionResult> Delete_Dictionary_Types(Dictionary_Request_Dto dto)
        {
            var result = await _dictionary_Service.Delete_Dictionary_Types(dto.id);
            return Ok(result);
        }
        /// <summary>
        /// 获取单个字典类别
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Single_Dictionary_Type(string id)
        {
            var result = await _dictionary_Service.Get_Single_Dictionary_Type(id);
            return Ok(result);
        }

        /// <summary>
        /// 删除字典[ids]
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete_Dictionarys(Dictionary_Request_Dto dto)
        {
            var result = await _dictionary_Service.Delete_Dictionarys(dto.id);
            return Ok(result);
        }

        /// <summary>
        /// 新增字典[name/value/type]
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        public async Task<IActionResult> Add_Dictionary(Dictionary_Request_Dto dto)
        {
            var user = await this.Get_Curr_User();
            dto.author = user.UId;
            var result = await _dictionary_Service.Add_Dictionary(dto);
            return Ok(result);
        }
        /// <summary>
        /// 修改字典类别
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Update_Dictionarys(string id,string? name,string? value,string? key)
        {
            var user = await this.Get_Curr_User();
            var result = await _dictionary_Service.Update_Dictionarys(new Dictionary_Response_Dto
            {
                update_author=user.UId,
                id=id,
                name=name,
                value=value,
                key=key
            });
            return Ok(result);
        }
        /// <summary>
        /// 新增子级字典
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        public async Task<IActionResult> Add_Dictionary_type(Dictionary_Request_Dto dto)
        {
            var user = await this.Get_Curr_User();
            dto.author = user.UId;
            var result = await _dictionary_Service.Add_Dictionary_type(dto);
            return Ok(result);
        }
    }
}
